package com.skymxc.wandroidtodo.adapter;

import android.hardware.lights.LightState;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.skymxc.wandroidtodo.R;
import com.skymxc.wandroidtodo.entity.TODOItem;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author 孟祥超
 * <p>
 * date: 2021/12/3  7:01 上午
 */
public class TODOItemAdapter extends RecyclerView.Adapter<TODOItemAdapter.TODOItemViewHolder> {
    private List<TODOItem> items;

    private ClickCallback clickCallback;

    public TODOItemAdapter(List<TODOItem> items) {
        this.items = items;
    }

    public void setClickCallback(ClickCallback clickCallback) {
        this.clickCallback = clickCallback;
    }

    @NonNull
    @Override
    public TODOItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_todo_item, parent, false);
        return new TODOItemViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull TODOItemViewHolder holder, int position) {
        TODOItem todoItem = items.get(position);
        holder.checkBoxStatus.setOnCheckedChangeListener(null);
        holder.checkBoxStatus.setChecked(todoItem.getStatus() == 1);
        holder.checkBoxStatus.setText(todoItem.getTitle());

        holder.checkBoxStatus.setTag(todoItem);
        holder.tvUpdate.setTag(todoItem);
        holder.tvDelete.setTag(todoItem);

        holder.tvDelete.setOnClickListener(onDeleteClick);
        holder.tvUpdate.setOnClickListener(onUpdateClick);
        holder.checkBoxStatus.setOnCheckedChangeListener(onCheckedChangeListener);
    }

    @Override
    public int getItemCount() {
        return null == items ? 0 : items.size();
    }

    public void add(List<TODOItem> todoItems) {
        int size = this.items.size();
        this.items.addAll(todoItems);
        notifyItemRangeInserted(size, todoItems.size());
    }

    public void removeItem(TODOItem item) {
        int indexOf = this.items.indexOf(item);
        if (indexOf > -1) {
            this.items.remove(indexOf);
            notifyItemRemoved(indexOf);
            notifyItemRangeChanged(indexOf, this.items.size());
        }
        Log.i("TODOItemAdapter.java", "removeItem: "+indexOf);
    }

    private View.OnClickListener onDeleteClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TODOItem item = (TODOItem) view.getTag();
            if (null != clickCallback) {
                clickCallback.onDeleteClick(item);
            }
        }
    };


    private View.OnClickListener onUpdateClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TODOItem item = (TODOItem) view.getTag();
            if (null != clickCallback) {
                clickCallback.onUpdateCLick(item);
            }
        }
    };

    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
            TODOItem item = (TODOItem) compoundButton.getTag();
            if (null != clickCallback) {
                clickCallback.onCheckChanged(item, isChecked);
            }
        }
    };

    static class TODOItemViewHolder extends RecyclerView.ViewHolder {

        public CheckBox checkBoxStatus;
        public TextView tvDelete;
        public TextView tvUpdate;

        public TODOItemViewHolder(@NonNull View itemView) {
            super(itemView);
            this.checkBoxStatus = itemView.findViewById(R.id.item_checkbox_status);
            this.tvDelete = itemView.findViewById(R.id.item_tv_delete);
            this.tvUpdate = itemView.findViewById(R.id.item_tv_update);
        }

    }

    public interface ClickCallback {
        void onDeleteClick(TODOItem item);

        void onUpdateCLick(TODOItem item);

        void onCheckChanged(TODOItem item, boolean isChecked);
    }
}
